<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/14
 * Time: 16:50
 */

namespace Admin\Controller;


class GoodssizeController extends AdminController
{
    //规格列表
    public function index(){
        $gid=I('gid');
        $uid=session('user_auth.uid');
        //审核权限
        if($uid!=1){
            $grout_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
        }
        $where['gid']=$gid;
        $listsCount = M('Goodssize')->where($where)->count();
        $Page       = new \Think\Page($listsCount,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $filed='';
        $lists = M('Goodssize')->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($lists as $ke=>$ve){
            $goods_info=M('Goods')->where(array('id'=>$ve['gid']))->find();
            $lists[$ke]['goods_name']=$goods_info['goods_name'];
        }
        $this->assign('list',$lists);
        $this->assign('gid',$gid);
        $this->assign('_page',$show);
        $this->display();
    }

    //添加规格
    public function add(){
        $gid=I('gid');
        if(IS_POST){
            $dde = D('Goodssize')->create();
            if($dde){
                //加入规格数据
                D('Goodssize')->add($_POST);
                $res=$this->update_price_type($gid,$_POST['price_type']);
                $this->success('添加成功',U('Goodssize/index?gid='.$gid));
            }else{
                $this->error(D('Goodssize')->getError());
            }
        }else{
            $list=M('Goods')->where(array('id'=>$gid))->find();
            $this->assign('list',$list);
            $this->display();
        }
    }

    //编辑规格
    public function edit(){
        $id=I('id');
        $size_info=M('Goodssize')->where(array('id'=>$id))->find();
        $goods_info=M('Goods')->where(array('id'=>$size_info['gid']))->find();
        if(IS_POST){
            $dde = D('Goods')->create();
            if($dde){
                D('Goodssize')->where(array("id"=>$_POST['id']))->save($_POST);
                $res=$this->update_price_type($size_info['gid'],$_POST['price_type']);
                $this->success('修改成功',U('Goodssize/index?gid='.$size_info['gid']));
            }else{
                $this->error(D('Goods')->getError());
            }
        }else{
            $size_info['goods_name']=$goods_info['goods_name'];
            $this->assign('list',$size_info);
            $this->display();
        }
    }

    //更新规格对应的商品数据
    public function update_price_type($gid,$type){
        $goods_info=M('goodssize')->where(array('gid'=>$gid))->select();
        $sales=0;
        //价格方式：1 详细价格 2 面议
        if($type==1){
            foreach ($goods_info as $key => $val){
                $sales+=$val['sales_volume'];
                $price[]=$val['goods_price'];
            }
            $max_price=max($price);
            $min_price=min($price);
            if($max_price!=$min_price){
                $goods_price=$min_price.'-'.$max_price;
            }else{
                $goods_price=$min_price;
            }
            //更新商品关联价格
            $res=M('Goods')->where(array('id'=>$gid))->save(array('goods_price'=>$goods_price,'goods_tel'=>'','sales_volume'=>$sales,'price_type'=>1,'update_time'=>time()));
        }else{
            foreach ($goods_info as $key => $val){
                $sales+=$val['sales_volume'];
                $tel=$val['goods_tel'];
            }
            //更新商品关联电话
            $res=M('Goods')->where(array('id'=>$gid))->save(array('goods_price'=>'','goods_tel'=>$tel,'sales_volume'=>$sales,'price_type'=>2,'update_time'=>time()));
        }
        return $res;
    }
}